package cn.dengta.webapp.user.form;

import cn.dengta.common.context.I18N;
import cn.dengta.common.util.Common;
import cn.dengta.common.validator.*;
import cn.dengta.context.model.UserI18nKey;
import cn.dengta.webapp.base.form.BaseForm;
import cn.dengta.webapp.user.entity.*;
import cn.dengta.webapp.user.entity.ComplainType.BizType;
import lombok.*;
import me.codeplayer.util.*;

import static cn.dengta.context.model.UserFormI18nKey.*;

@Getter
@Setter
public class ComplainForm extends BaseForm<Complain> {

	@Check(COMPLAIN_FORM_TYPE)
	private BizType type;
	@Check(COMPLAIN_FORM_TYPE_ID)
	@Range(min = 1)
	private Long typeId;
	/** 图片地址 */
	@Check(value = COMPLAIN_FORM_IMAGES, required = false)
	@Trim
	@NotEmpty
	private String images;
	/** 内容 */
	@Check(value = COMPLAIN_FORM_CONTENT, required = false)
	@Trim
	@NotEmpty
	@Size(max = 600)
	private String content;
	/** 被举报人 */
	@Check(value = COMPLAIN_FORM_TO_USER_ID, required = false)
	@Range(min = 1)
	private Long toUserId;
	@Check(value = "ID", i18n = false, required = false)
	@Range(min = 1)
	private Long assocId;
	@Setter(AccessLevel.NONE)
	private String assocType;

	@Override
	public void validate() {
		super.validate();
		I18N.assertNotNull(type, UserI18nKey.COMPLAIN_TYPE_REQUIRED);
		if (type != BizType.User && type != BizType.GROUP) {
			Assert.isTrue(StringUtil.notEmpty(assocType));
			Assert.isTrue(assocId != null);
		}
		switch (type) {
			case User:
				Assert.isTrue(X.size(Common.splitAsStringList(images)) <= 5, () -> I18N.msg(UserI18nKey.COMPLAIN_LIMIT_PHOTO, 5));
				Assert.isTrue(StringUtil.isEmpty(content) || content.length() <= 600, () -> I18N.msg(UserI18nKey.COMPLAIN_CONTENT_LIMIT, 600));
				assocType = User.class.getSimpleName();
				assocId = toUserId;
				break;
			case Moment:
				Assert.isTrue(StringUtil.isEmpty(images));
				Assert.isTrue(StringUtil.isEmpty(content) || content.length() <= 200, () -> I18N.msg(UserI18nKey.COMPLAIN_CONTENT_LIMIT, 200));
				break;
			case WORD:
			case VIDEO:
				break;
			case GROUP:
				assocType = BizType.GROUP.name();
				assocId = toUserId;
				break;
			default:
				throw new IllegalArgumentException();
		}
	}

	public void setType(Integer type) {
		this.type = BizType.of(type);
	}

	public void setMomentId(Long momentId) {
		this.assocId = momentId;
		this.assocType = Moment.class.getSimpleName();
	}

	public void setCallId(Long callId) {
		this.assocId = callId;
		this.assocType = "MediaLog";
	}

}
